package com.pjj.projectservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjj.projectservice.entity.ProjectNote;
import com.pjj.projectservice.mapper.ProjectNoteMapper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 76004
 * @apiNote
 * @date 2023/2/16 18:42
 */
@Service
public class ProjectNoteService {
    @Value(value = "${imagePath}")
    private String imagePath;

    @Value(value = "${ipAdd}")
    private String ipAdd;
    @Autowired
    private ProjectNoteMapper projectNoteMapper;

    @Transactional
    public Object insertProjectNote(ProjectNote projectNote, MultipartFile imageFile) throws IOException {
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","添加成功");
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //生成图片保存文件和图片名称
        projectNote.setUrl(ipAdd+localDate.format(formatter)+"/"+String.valueOf(new Date().getTime())+".jpg");
        map.put("url",projectNote.getUrl());
        projectNoteMapper.insert(projectNote);
        //保存图片
        saveImage(projectNote.getUrl(),imageFile);
        return map;
    }

    public Object deleteById(Long id){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","删除成功");
        projectNoteMapper.deleteById(id);
        return map;
    }

    public Object updateProjectNote(ProjectNote projectNote){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","更新成功");
        projectNoteMapper.updateById(projectNote);
        return map;
    }

    public Object selectById(Long id){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        map.put("data",projectNoteMapper.selectById(id));
        return map;
    }

    public Object selectPage(int pageNum, int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        IPage<ProjectNote> page = projectNoteMapper.selectPage(new Page<>(pageNum,pageSize),null);
        map.put("data",page.getRecords());
        return map;
    }

    public Object selectByProjectId(Long projectId,int pageNum,int pageSize){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<ProjectNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("projectId",projectId);
        map.put("code",100);
        map.put("msg","查询成功");
        IPage<ProjectNote> page = projectNoteMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
        map.put("data",page.getRecords());
        return map;
    }

    public void saveImage(String url, MultipartFile file) throws IOException {
        file.transferTo(new File(imagePath+url));
    }

    public Object getImage(String url) throws IOException {
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        File file = new File(imagePath+url);
        byte[] imageData = FileUtils.readFileToByteArray(file);
        map.put("data",imageData);
        return map;
    }


}
